package com.monco;

import java.util.concurrent.Flow;

/**
 * @author : monco
 * @date : 2019/10/11 1:46
 * className: DockerXDemoSubscriber
 * description: 订阅人
 */
public class DockerXDemoSubscriber<T> implements Flow.Subscriber<T> {

    private String name;

    private Flow.Subscription subscription;

    final long bufferSize;

    long count;

    public DockerXDemoSubscriber(long bufferSize, String name) {
        this.name = name;
        this.bufferSize = bufferSize;
    }

    public String getName() {
        return name;
    }

    public Flow.Subscription getSubscription() {
        return subscription;
    }

    @Override
    public void onSubscribe(Flow.Subscription subscription) {
        (this.subscription = subscription).request(bufferSize);
        System.out.println("开始 onSubscribe 订阅 ");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onNext(T item) {
        System.out.println("######" + Thread.currentThread().getName() + "name " + name + "item " + item + "#####");
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onError(Throwable throwable) {
        throwable.printStackTrace();
    }

    @Override
    public void onComplete() {
        System.out.println("Completed!!!");
    }
}
